package editor.manager;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JToggleButton;

import editor.Context;
import editor.Mode;
import editor.layout.dialog.BuildProjectDialog;
import editor.layout.dialog.ExecuteProjectDialog;
import editor.project.ProjectFile;
import editor.tester.TestSettingsPanel;

/**
 * Manages the sidebar
 * 
 * @author Mark Dessain
 */
@SuppressWarnings("serial")
public class SideBarManager extends JPanel implements ActionListener, Manager
{
	
	private TestSettingsPanel testSettingsPanel;
	
	public final static ProjectManager projectManager = new ProjectManager();
	
	private JPanel modeButtonsPane = new JPanel();
	private JPanel mainPane = new JPanel();
	private JPanel jwalkPane = new JPanel();
	private JPanel buttonsPane = new JPanel();
	
	private JToggleButton                   codingMode              = new JToggleButton("Editor");
	private JToggleButton                   testingMode             = new JToggleButton("Testing Results");
	
	private JButton                         testClass               = new JButton("Test Class");
	private JButton                         executeProject          = new JButton("Execute Project");
	private JButton                         compileProject          = new JButton("Compile Project");
		
	/**
	 * Sidebar Manager
	 */
	public SideBarManager()
	{
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 0));
		
		codingMode.setSelected(true);
		
		testSettingsPanel = new TestSettingsPanel();
	
		modeButtonsPane.setLayout(new BoxLayout(modeButtonsPane, BoxLayout.X_AXIS));
		modeButtonsPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		modeButtonsPane.add(codingMode);
		modeButtonsPane.add(Box.createHorizontalGlue());
		modeButtonsPane.add(testingMode);
	
		buttonsPane.setLayout(new BoxLayout(buttonsPane, BoxLayout.X_AXIS));
		buttonsPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		buttonsPane.add(testClass);
		buttonsPane.add(Box.createHorizontalGlue());
		buttonsPane.add(executeProject);
		buttonsPane.add(Box.createHorizontalGlue());
		buttonsPane.add(compileProject);
				
		jwalkPane.setLayout(new BoxLayout(jwalkPane, BoxLayout.Y_AXIS));
		jwalkPane.add(testSettingsPanel, BorderLayout.NORTH);
		//jwalkPane.add(customPanel, BorderLayout.CENTER);
		jwalkPane.add(buttonsPane, BorderLayout.SOUTH);
		
		mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
		mainPane.add(modeButtonsPane, BorderLayout.NORTH);
		mainPane.add(projectManager, BorderLayout.SOUTH);
		
		JSplitPane projectSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT, mainPane, jwalkPane);
		projectSplit.setResizeWeight(1D);
		projectSplit.setEnabled(false);
		
		add(projectSplit);
		
		codingMode.addActionListener(this);
		testingMode.addActionListener(this);
		testClass.addActionListener(this);
		compileProject.addActionListener(this);
		executeProject.addActionListener(this);
		
	}

	/**
	 * @return the settings panel
	 */
	public TestSettingsPanel getTestSettingsPanel()
	{
		return testSettingsPanel;
	}
		
	/**
	 * @return the project manager
	 */
	public ProjectManager getProjectManager()
	{
		return projectManager;		
	}
	
	/**
	 * @param enable (or disable) the test class button
	 */
	public void setEnableTestClass(boolean enable)
	{
		testClass.setEnabled(enable);
	}
	
	/**
	 * @param enable (or disable) the execute class button
	 */
	public void setEnableExecuteProject(boolean enable)
	{
		executeProject.setEnabled(enable);
	}
	
	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		Object source = e.getSource();
		
		if(source == codingMode)
		{
			Context.switchMode(Mode.CODING);		
		}
		else if(source == testingMode)
		{
			Context.switchMode(Mode.TESTING);			
		}
		else if(source == testClass)
		{
			int index = WorkSpaceManager.textAreaManager.getSelectedIndex();
			
			if(index > -1)
			{
				ProjectFile editor = WorkSpaceManager.textAreaManager.getFileAt(index);
				
				if(editor != null)
				{
					WorkSpaceManager.testingManager.run(editor);
				}
			}
		}
		else if(source == compileProject)
		{
			new BuildProjectDialog();
		}
		else if(source == executeProject)
		{
			new ExecuteProjectDialog();
		}
	}
	
	/* (non-Javadoc)
	 * @see editor.manager.Manager#switchMode(editor.Mode)
	 */
	@Override 
	public void switchMode(Mode mode)
	{
		if(mode == Mode.CODING)
		{
			codingMode();
		}
		else if(mode == Mode.QUESTION)
		{
			questionMode();	
		}
		else if(mode == Mode.TESTING)
		{
			testingMode();
		}
		
		projectManager.switchMode(mode);
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#codingMode()
	 */
	@Override
	public void codingMode() 
	{
		codingMode.setSelected(true);
		testingMode.setSelected(false);
		
		codingMode.setEnabled(true);
		testingMode.setEnabled(true);
		
		testClass.setEnabled(true);
		executeProject.setEnabled(true);
		compileProject.setEnabled(true);
	}
	
	/* (non-Javadoc)
	 * @see editor.manager.Manager#questionMode()
	 */
	@Override
	public void questionMode()
	{
		codingMode.setSelected(true);
		testingMode.setSelected(false);

		codingMode.setEnabled(false);
		testingMode.setEnabled(false);
		
		testClass.setEnabled(false);
		executeProject.setEnabled(false);
		compileProject.setEnabled(false);
	}

	/* (non-Javadoc)
	 * @see editor.manager.Manager#testingMode()
	 */
	@Override
	public void testingMode() 
	{
		codingMode.setSelected(false);
		testingMode.setSelected(true);

		codingMode.setEnabled(true);
		testingMode.setEnabled(true);
		
		testClass.setEnabled(false);
		executeProject.setEnabled(false);
		compileProject.setEnabled(false);
	}
	

}
